﻿Option Strict Off
Option Explicit On
Module ConectaBombas
    Public WithEvents T2000 As TEAM.SerialTXRX.SerialDeviceProtocols
    Public TipoDispensarios As String
    Public EstadoDispensario As String
    Public VentaDispensario As String
    Public LecturaPrecio As String
    Public LecturaTotal As String
    Public PrefijadoDispensario As String
    Public cveusr01 As String
    Public cveofna As String
    Public OFNLectora As String
    Public USUMANGLO As Integer
    Public nomofna As String
    Public TIPotar As String
    Public NUMetar As Integer
    Public TIPOReg As String
    Public ADM As String
    Public TipoSurtido As String
    Public pasadesbloqueo As Short
    Public IdenTur As Integer
    Public IdenTur_1 As Integer
    Public Autobus_1 As Integer
    Public Distancia_1 As Integer
    Public Rendimiento_1 As Double
    Public IdenTur_2 As Integer
    Public Autobus_2 As Integer
    Public Distancia_2 As Integer
    Public Rendimiento_2 As Double
    Public IdenTur_3 As Integer
    Public Autobus_3 As Integer
    Public Distancia_3 As Integer
    Public Rendimiento_3 As Double
    Public IdenTur_4 As Integer
    Public Autobus_4 As Integer
    Public Distancia_4 As Integer
    Public Rendimiento_4 As Double
    Public IdenTur_5 As Integer
    Public Autobus_5 As Integer
    Public Distancia_5 As Integer
    Public Rendimiento_5 As Double
    Public IdenTur_6 As Integer
    Public Autobus_6 As Integer
    Public Distancia_6 As Integer
    Public Rendimiento_6 As Double
    Public IdenTur_7 As Integer
    Public Autobus_7 As Integer
    Public Distancia_7 As Integer
    Public Rendimiento_7 As Double
    Public IdenTur_8 As Integer
    Public Autobus_8 As Integer
    Public Distancia_8 As Integer
    Public Rendimiento_8 As Double
    Public ContadorDiesel As Integer
    Public Rendimiento As Double
    Public Distancia As Integer
    Public IniDobleAncho As String
    Public FinDobleAncho As String
    Public IniDobleAlto As String
    Public FinDobleAlto As String
    Public CortePapel As String

    Public Const NumDispensario As Short = 1
    Public Const NumLado As Short = 2
    Public Const StatusDispensario As Short = 7
    Public Const LitrosVenta As Short = 4
    Public Const LitrosPrefijados As Short = 4
    Public Const PrecioDiesel As Short = 4
    Public Const LitrosTotales As Short = 3
    Public Const VtaDiesel As Short = 3

    Public Delegate Sub ExecT2000_ReceiveData(ByVal data() As String)
    Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Integer, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Integer) As Integer
    Declare Function WinExec Lib "Kernel32.dll" (ByVal lpCmdLine As String, ByVal nCmdShow As Short) As Short
    Declare Sub Sleep Lib "Kernel32.dll" (ByVal dwMilliseconds As Integer)
    Declare Function INITPORT Lib "C:\windows\system\Rs485_TeamUSB.dll" (ByVal N As Integer, ByVal N1 As Integer) As Integer

    Dim Respuesta, Ayuda, Estilo, Mensaje, Título, Ctxt, MiCadena As Object

    Structure terceros ' Crea un tipo definido por el usuario.
        Dim placas As String
        Dim numope As Integer
        Dim nombre As String
        Dim carro As Short
        Dim siglas As String
        Dim litsur As Short
        Dim tipmov As String
        Dim Kilometros As Integer
        Dim NumSelloAnt As String
        Dim NumSelloAct As String
        Dim memNumope As Integer
        Dim memCarro As Short
        Dim memSiglas As String
        Dim memNombre As String
        Dim ADMIN As String
        Dim TIPOREGISTRO As String
        Dim TIPOUSUARIO As String
        Dim NUMROL As String
        Dim USUMAN As Integer
    End Structure

    Structure grupos ' Crea un tipo definido por el usuario.
        Dim numaut As Short
        Dim siglas As String
        Dim litsur As Short
        Dim numtar As Integer
        Dim numval As Short
        Dim numope As Integer
        Dim nomope As String
        Dim Kilometros As Integer
        Dim NumSelloAnt As String
        Dim NumSelloAct As String
        Dim ADMIN As String
        Dim TIPOREGISTRO As String
        Dim TIPOUSUARIO As String
        Dim NUMROL As String
        Dim USUMAN As Integer
    End Structure

    'Public dsNeo As New DataEnvironment_deNeo
    Public ter01 As terceros
    Public ter02 As terceros
    Public ter03 As terceros
    Public ter04 As terceros
    Public ter05 As terceros
    Public ter06 As terceros
    Public ter07 As terceros
    Public ter08 As terceros

    Public tipmov As String
    Public grp01(10) As grupos
    Public grp02(10) As grupos
    Public grp03(10) As grupos
    Public grp04(10) As grupos
    Public grp05(10) As grupos
    Public grp06(10) As grupos
    Public grp07(10) As grupos
    Public grp08(10) As grupos

    Public usuario As Integer
    Public aplic As String
    Public despini, espimp, esplit, espcar, despcon As String
    Public despfin As String
    Public lit(8) As Double
    Public importe(8) As Double
    Public vta(8) As Double
    Public carros(8) As Double
    Public tar01(10) As Double
    Public lit01(10) As Double
    Public cif01(10) As Double



    Public JDENombreOficina As String
    Public MCU_ERP As String
    Public turno1 As String
    Public turno2 As String
    Public turno3 As String

    Public Function VentaLitros(ByRef manguera As Short) As Double
        Dim Respuesta As String
        Dim Comando As Short
        Dim Dispensario As Short
        Dim Lado As Short
        Dim Dato(9) As String
        Dim ExecT2000 As ExecT2000_ReceiveData
        On Error GoTo ErrorSalida
        If TipoDispensarios = "T2000" Then
            VentaDispensario = "-99"
            Dispensario = Int((manguera + 1) / 2)
            Lado = 2 - manguera Mod 2
            Sleep(100)
            T2000.obtenerVenta_T2000(Dispensario, Lado, True)
            ExecT2000 = AddressOf T2000_ReceiveData
            Sleep(100)
            ExecT2000.Invoke(Dato)
            If Val(Left(VentaDispensario, 2)) = Dispensario And Val(Mid(VentaDispensario, 3, 2)) = Lado Then
                If LeerEstatus(manguera) = 0 Then
                    VentaLitros = Val(Mid(VentaDispensario, 5))
                Else
                    VentaLitros = -Val(Mid(VentaDispensario, 5))
                End If
            Else
                VentaLitros = -1
            End If
        Else
            Comando = &H81 ' Leer Estatus
            Respuesta = EnvioComando(manguera, Comando, "0", "0", "0", True)
            If Right("0" & Trim(Str(manguera)), 2) = Left(Respuesta, 2) And "81" = Mid(Respuesta, 4, 2) Then ' And Mid(Respuesta, 7, 2) = "C0" Then
                If Mid(Respuesta, 7, 2) = "C0" Then
                    VentaLitros = Val(Mid(Respuesta, 10, 2) & Mid(Respuesta, 13, 2) & Mid(Respuesta, 16, 2)) / 100
                Else
                    VentaLitros = -Val(Mid(Respuesta, 10, 2) & Mid(Respuesta, 13, 2) & Mid(Respuesta, 16, 2)) / 100
                End If
            Else
                VentaLitros = -1
            End If
        End If
        Exit Function
ErrorSalida:
        VentaLitros = -1
    End Function

    Public Function VentaPesos(ByRef manguera As Short) As Double
        Dim Respuesta As String
        Dim Comando As Short
        Comando = &H89 ' Lectura venta actual en pesos
        Respuesta = EnvioComando(manguera, Comando, "0", "0", "0", True)
        If Right("0" & Trim(Str(manguera)), 2) = Left(Respuesta, 2) And "89" = Mid(Respuesta, 4, 2) Then ' And Mid(Respuesta, 7, 2) = "C0" Then
            If Mid(Respuesta, 7, 2) = "C0" Then
                VentaPesos = Val(Mid(Respuesta, 10, 2) & Mid(Respuesta, 13, 2) & Mid(Respuesta, 16, 2) & Mid(Respuesta, 19, 2)) / 1000
            Else
                VentaPesos = -Val(Mid(Respuesta, 10, 2) & Mid(Respuesta, 13, 2) & Mid(Respuesta, 16, 2) & Mid(Respuesta, 19, 2)) / 1000
            End If
        Else
            VentaPesos = -1
        End If
    End Function

    Public Function VENTATOTAL(ByRef manguera As Short) As Double
        Dim Respuesta As String
        Dim Comando As Short
        Dim Dispensario As Short
        Dim Lado As Short
        Dim Dato(9) As String
        Dim ExecT2000 As ExecT2000_ReceiveData
        If TipoDispensarios = "T2000" Then
            LecturaTotal = "-99"
            Dispensario = Int((manguera + 1) / 2)
            Lado = 2 - manguera Mod 2
            T2000.consultarTotales_T2000(Dispensario, Lado, VtaDiesel)
            ExecT2000 = AddressOf T2000_ReceiveDataCorteDeTurno 'T2000_ReceiveData
            Sleep(100)
            ExecT2000.Invoke(Dato)
            If Val(Left(LecturaTotal, 2)) = Dispensario And Val(Mid(LecturaTotal, 3, 2)) = Lado Then
                VENTATOTAL = Val(Mid(LecturaTotal, 5))
            Else
                VENTATOTAL = -1
            End If
        Else
            Comando = &H85 ' Lectura venta acumulada litros
            Respuesta = EnvioComando(manguera, Comando, "0", "0", "0", True)
            If Right("0" & Trim(Str(manguera)), 2) = Left(Respuesta, 2) And "85" = Mid(Respuesta, 4, 2) Then
                VENTATOTAL = Val(Mid(Respuesta, 10, 2) & Mid(Respuesta, 13, 2) & Mid(Respuesta, 16, 2) & Mid(Respuesta, 19, 2)) / 100
            Else
                VENTATOTAL = -1
            End If
        End If
    End Function

    Public Function BorraTotal(ByRef manguera As Short) As Double
        Dim Respuesta As String
        Dim Comando As Short
        Dim Dispensario As Short
        If TipoDispensarios = "T2000" Then
            Dispensario = Int((manguera + 1) / 2)
            T2000.borrarTotales_T2000(Dispensario)
            BorraTotal = 0
        Else
            Comando = &H83 ' Borra acumulado litros
            Respuesta = EnvioComando(manguera, Comando, "0", "0", "0", True)
            If Right("0" & Trim(Str(manguera)), 2) = Left(Respuesta, 2) And "83" = Mid(Respuesta, 4, 2) And Mid(Respuesta, 10, 11) = "00 00 00 00" Then
                BorraTotal = 0
            Else
                BorraTotal = -1
            End If
        End If
    End Function

    Public Function LeerEstatus(ByRef manguera As Short) As Short
        Dim Respuesta As String
        Dim Comando As Short
        Dim Dispensario As Short
        Dim Lado As Short
        Dim Dato(9) As String
        Dim ExecT2000 As ExecT2000_ReceiveData
        If TipoDispensarios = "T2000" Then
            EstadoDispensario = "-99"
            Dispensario = Int((manguera + 1) / 2)
            Lado = 2 - manguera Mod 2
            T2000.C_statusDispensario_T2000(Dispensario, Lado)
            ExecT2000 = AddressOf T2000_ReceiveData
            Sleep(100)
            ExecT2000.Invoke(Dato)
            If Val(Left(EstadoDispensario, 2)) = Dispensario And Val(Mid(EstadoDispensario, 3, 2)) = Lado Then
                LeerEstatus = Val(Mid(EstadoDispensario, 5))
            Else
                LeerEstatus = -2
            End If
        Else
            Comando = &H81 ' Leer Estatus
            Respuesta = EnvioComando(manguera, Comando, "0", "0", "0", True)
            'frmconfig.List1.AddItem(Respuesta)
            If Right("0" & Trim(Str(manguera)), 2) = Left(Respuesta, 2) And "81" = Mid(Respuesta, 4, 2) Then
                Select Case Mid(Respuesta, 7, 2) ' Evalúa Estatus.
                    Case "C0" ' Manguera Disponible Bloqueada
                        LeerEstatus = 0
                    Case "C1" ' Manguera Ocupada Bloqueada
                        LeerEstatus = 1
                    Case "40" ' Manguera Disponible
                        LeerEstatus = -1
                    Case Else ' Manguera Ocupada
                        LeerEstatus = -2
                End Select
            Else
                LeerEstatus = -3
            End If
        End If
    End Function

    Public Function Bloquear(ByRef manguera As Short) As Short
        Dim Respuesta As String
        Dim Comando As Short
        Dim Dispensario As Short
        If TipoDispensarios = "T2000" Then
            Dispensario = Int((manguera + 1) / 2)
            T2000.autoServicio_T2000(Dispensario, True)
            Bloquear = 0
        Else
            Comando = &H82 ' Bloquear o Desbloquear manguera
            Respuesta = CStr(LeerEstatus(manguera))
            If Respuesta = "-1" Then
                Respuesta = EnvioComando(manguera, Comando, "0", "0", "0", True)
                If Right("0" & Trim(Str(manguera)), 2) = Left(Respuesta, 2) And "82" = Mid(Respuesta, 4, 2) Then
                    If Mid(Respuesta, 7, 2) = "C0" Then
                        Bloquear = 0
                    Else
                        Bloquear = -1
                    End If
                End If
            Else
                Bloquear = -2
            End If
        End If
    End Function

    Public Function Desbloquear(ByRef manguera As Short) As Short
        Dim Respuesta As String
        Dim Comando As Short
        Dim Dispensario As Short
        If TipoDispensarios = "T2000" Then
            Dispensario = Int((manguera + 1) / 2)
            T2000.autoServicio_T2000(Dispensario, False)
            Desbloquear = 0
        Else
            Comando = &H82 ' Bloquear o Desbloquear manguera
            Respuesta = CStr(LeerEstatus(manguera))
            If Respuesta = "0" Then
                Respuesta = EnvioComando(manguera, Comando, "0", "0", "0", True)
                If Right("0" & Trim(Str(manguera)), 2) = Left(Respuesta, 2) And "82" = Mid(Respuesta, 4, 2) Then
                    If Mid(Respuesta, 7, 2) = "40" Then
                        Desbloquear = 0
                    Else
                        Desbloquear = -1
                    End If
                End If
            Else
                Desbloquear = -2
            End If
        End If
    End Function

    Public Function LeerPrecio(ByRef manguera As Short) As Double
        Dim Respuesta As String
        Dim Comando As Short
        Dim Dispensario As Short
        Dim Dato(9) As String
        Dim ExecT2000 As ExecT2000_ReceiveData
        If TipoDispensarios = "T2000" Then
            LecturaPrecio = "-99"
            Dispensario = Int((manguera + 1) / 2)
            T2000.leerPrecios_T2000(Dispensario)
            ExecT2000 = AddressOf T2000_ReceiveDataLeerPrecios 'T2000_ReceiveData 
            Sleep(100)
            ExecT2000.Invoke(Dato)
            If Val(Left(LecturaPrecio, 2)) = Dispensario Then
                LeerPrecio = Val(Mid(LecturaPrecio, 3))
            Else
                LeerPrecio = -1
            End If
        Else
            Comando = &H87 ' Lectura precio
            Respuesta = EnvioComando(manguera, Comando, "0", "0", "0", True)
            If Right("0" & Trim(Str(manguera)), 2) = Left(Respuesta, 2) And "87" = Mid(Respuesta, 4, 2) Then
                LeerPrecio = Val(Mid(Respuesta, 10, 2) & Mid(Respuesta, 13, 2) & Mid(Respuesta, 16, 2)) / 1000
            Else
                LeerPrecio = -1
            End If
        End If
    End Function

    Public Function ActualizacionPrecio(ByRef manguera As Short, ByRef precio As Double) As Short
        Dim Respuesta As String
        Dim PrecioStr As String
        Dim Comando As Short
        Dim Dispensario As Short
        If TipoDispensarios = "T2000" Then
            PrecioStr = Left(Right("0000" & Trim(Str(100 * precio)), 4), 2) + "." + Right(Right("0000" & Trim(Str(100 * precio)), 4), 2)
            Dispensario = Int((manguera + 1) / 2)
            T2000.cambiarPrecios_T2000(Dispensario, PrecioStr + "," + PrecioStr + "," + PrecioStr)
            ActualizacionPrecio = 0
        Else
            PrecioStr = Right("000000" & Trim(Str(1000 * precio)), 6)
            Comando = &H88 ' Cambio Precio
            Respuesta = EnvioComando(manguera, Comando, Left(PrecioStr, 2), Mid(PrecioStr, 3, 2), Right(PrecioStr, 2), False)
            Comando = &H87 ' Leer precio para verificar cambio
            Respuesta = EnvioComando(manguera, Comando, "0", "0", "0", True)
            If Right("0" & Trim(Str(manguera)), 2) = Left(Respuesta, 2) And "87" = Mid(Respuesta, 4, 2) And Left(PrecioStr, 2) = Mid(Respuesta, 10, 2) And Mid(PrecioStr, 3, 2) = Mid(Respuesta, 13, 2) And Right(PrecioStr, 2) = Mid(Respuesta, 16, 2) Then
                ActualizacionPrecio = 0
            Else
                ActualizacionPrecio = -1
            End If
        End If
    End Function

    Public Function PrefijarLitros(ByRef manguera As Short, ByRef Litros As Double) As Short
        Dim Respuesta As String
        Dim LitrosStr As String
        Dim Comando As Short
        Dim Dispensario As Short
        Dim Lado As Short
        If TipoDispensarios = "T2000" Then
            Dispensario = Int((manguera + 1) / 2)
            Lado = 2 - manguera Mod 2
            T2000.Prefijar_T2000(Dispensario, Lado, Litros, True)
            PrefijarLitros = 0
        Else
            LitrosStr = Right("000000" & Trim(Str(100 * (Litros - 0.5))), 6)
            Comando = &H86 ' Prefijar Litros
            Respuesta = EnvioComando(manguera, Comando, Left(LitrosStr, 2), Mid(LitrosStr, 3, 2), Right(LitrosStr, 2), True)
            If Right("0" & Trim(Str(manguera)), 2) = Left(Respuesta, 2) And "86" = Mid(Respuesta, 4, 2) Then
                PrefijarLitros = 0
            Else
                PrefijarLitros = -1
            End If
        End If
    End Function
    Public Function EnvioComando(ByRef manguera As Short, ByRef Comando As Short, ByRef Dato1 As String, ByRef Dato2 As String, ByRef Dato3 As String, ByRef Respuesta As Boolean) As String
        Dim contador As Short
        Dim CRCByte As String
        Dim Buffer_In, Salida As String
        Dim Suma As String
        Dim NumCaracteres As Short
        Dim DataInput As Char()
        On Error GoTo ErrorSalida
        Suma = CStr(CDbl(Hex(manguera)) + Comando + Val("&H" & Dato1) + Val("&H" & Dato2) + Val("&H" & Dato3))
        CRCByte = Hex(Not (Suma))
        Salida = Chr(Val(CStr(manguera))) & Chr(CShort(Comando)) & Chr(CShort("&H" & Dato1)) & Chr(CShort("&H" & Dato2)) & Chr(CShort("&H" & Dato3)) & Chr(0) & Chr(0) & Chr(CShort("&H" & Right(CRCByte, 2)))
        frmPruebasDiesel.commUSB.Write(Salida)
        'frmLogin.commUSB.Write(Salida.ToCharArray, 0, 8)
        contador = frmPruebasDiesel.commUSB.BytesToWrite
        Sleep((1000))
        'If frmLogin.commUSB1.InBufferCount Then
        NumCaracteres = frmPruebasDiesel.commUSB.BytesToRead
        'End If
        'If frmLogin.commUSB.BytesToRead > 0 Then
        'NumCaracteres = frmLogin.commUSB.BytesToRead
        'End If
        EnvioComando = ""
        If Respuesta = False Then
            EnvioComando = "0"
        Else
            'If NumCaracteres > 2 Then
            DataInput = ""
            Buffer_In = frmPruebasDiesel.commUSB.ReadExisting
            'contador = frmLogin.commUSB.Read(DataInput, offset:=0, count:=8)
            Buffer_In = DataInput.ToString
            contador = NumCaracteres - 1
            Suma = CStr(0)
            Do While contador > 0
                Suma = CStr(CDbl(Suma) + Asc(Mid(Buffer_In, contador, 1)))
                contador = contador - 1
            Loop
            CRCByte = Hex(Not (Suma))
            If Chr(CShort("&H" & (Right(CRCByte, 2)))) = Mid(Buffer_In, NumCaracteres, 1) Then
                For contador = 1 To NumCaracteres - 1
                    EnvioComando = EnvioComando & Right("00" & Hex(Asc(Mid(Buffer_In, contador, 1))), 2) & " "
                    'frmconfig.List1.AddItem (EnvioComando)
                Next contador
            End If
            'End If
        End If
        Exit Function
ErrorSalida:
        EnvioComando = "-1"


    End Function

    Private Sub T2000_ReceiveData(ByVal data() As String) Handles T2000.ReceiveData
        On Error GoTo ErrorSalida
            Select data(0)
            Case "A1"
                VentaDispensario = data(NumDispensario) & data(NumLado) & data(LitrosVenta)
            Case "A3"
                EstadoDispensario = data(NumDispensario) & data(NumLado) & data(StatusDispensario)
            Case "A5"
                PrefijadoDispensario = data(NumDispensario) & data(NumLado) & data(LitrosPrefijados)
            Case "A6"
                LecturaPrecio = data(NumDispensario) & data(PrecioDiesel)
            Case "A9"
                LecturaTotal = data(NumDispensario) & data(NumLado) & data(LitrosTotales)
            Case Else
                Debug.WriteLine("No se sabe")
        End Select
        Exit Sub
ErrorSalida:
        VentaDispensario = "-99"
        EstadoDispensario = "-99"
        PrefijadoDispensario = "-99"
        LecturaPrecio = "-99"
        LecturaTotal = "-99"
    End Sub
    Private Sub T2000_ReceiveDataCorteDeTurno(ByVal data() As String) Handles T2000.ReceiveDataCorteDeTurno
               Select data(0)
            Case "A9"
                LecturaTotal = data(NumDispensario) & data(NumLado) & data(LitrosTotales)
            Case Else
                '              Debug.WriteLine("No se sabe")
        End Select
    End Sub

    Private Sub T2000_ReceiveDataLeerPrecios(ByVal data() As String) Handles T2000.ReceiveDataLeerPrecios
            Select data(0)
            Case "A6"
                LecturaPrecio = data(NumDispensario) & data(PrecioDiesel)
            Case Else
                Debug.WriteLine("No se sabe")
        End Select
    End Sub

End Module
